package com.zelin.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.*;

/**
 * Created by WF on 2020/12/27 21:25
 * 定义校验器对象
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> { //泛型1：代表验证规则的类 泛型2：代表要验证字段的类型
    Set<Integer> sets = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        //1.得到传入的参数值
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            sets.add(val);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return sets.contains(value);
    }
}
